package com.eptok.yspay.opensdkjava.merc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.eptok.yspay.opensdkjava.common.Constants;
import com.eptok.yspay.opensdkjava.pojo.vo.OnlineReqDataVo;
import com.eptok.yspay.opensdkjava.pojo.vo.YsfReqDataVo;
import com.eptok.yspay.opensdkjava.util.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 线上商户进件接口
 */
public class OnlineMercApi {

    private static final Logger logger = Logger.getLogger("OnlineMercApi");
    private final static String version = "3.0";

    /**
     *  线上商户获取图片上传口令接口
     */
    public static String getToken(OnlineReqDataVo vo) throws Exception {

        /** 2、组装入参的数据*/
        Map<String, String> params = new HashMap<String, String>();
        params.put("method","ysepay.merchant.register.token.get");
        params.put("partner_id",vo.getPartnerId());
        params.put("timestamp", DateUtil.getDateNow());
        params.put("charset", Constants.CHARSET_UTF_8);
        params.put("sign_type",vo.getSignType());
        params.put("notify_url",vo.getNotifyUrl());
        params.put("version",version);
        //获取token没有业务参数，因此写成{}
        params.put("biz_content","{}");

        /*3、对数据进行加签，并存入请求数据*/
        String sign = YsOnlineSignUtils.sign(params, vo.getPrivateKeyPassword(),vo.getPrivateKeyFilePath());
        logger.info("OnlineMercApi-getToken-signData:"+sign);
        params.put("sign",sign);

        /*4、发送请求*/
        String result = HttpClientUtil.sendPostParam(vo.getReqUrl(),StringUtil.mapToString(params));
        logger.info("OnlineMercApi-getToken-resultData:"+result);
        if(StringUtil.isEmpty(result)){
            logger.info("OnlineMercApi getToken resultData is empty");
        }
        /*5、结果验签*/
        JSONObject jsonObject= JSON.parseObject(result, Feature.OrderedField);
        // 加签值
        String sinaValue = (String) jsonObject.get("sign");
        // 加签内容
        String content = jsonObject.get("ysepay_merchant_register_token_get_response").toString();
        boolean flag = YsOnlineSignUtils.rsaCheckContent(content,sinaValue,Constants.CHARSET_UTF_8,vo.getYsPublicKeyFilePath());
        if(!flag){
            throw new Exception("check sign error");
        }
        return content;
    }

    /**
     *  线上商户图片上传接口
     */
    public static String uploadPicture(OnlineReqDataVo vo, byte[] fileByte, String fileName) throws Exception {

        /** 1、组装需要加签的入参的数据*/
        HashMap<String, String> param = new HashMap<String, String>();
        param.put("picType", (String) vo.getParamData().get("picType"));
        param.put("token", (String) vo.getParamData().get("token"));
        param.put("superUsercode", (String) vo.getParamData().get("superUsercode"));

        /*3、发送请求*/
        String result = HttpClientUtil.sendPostFile(vo.getReqUrl(),param,fileByte,fileName,"picFile");
        logger.info("OnlineMercApi-uploadPicture-resultData:"+result);
        return result;
    }

    /**
     *  线上商户注册接口
     */
    public static String merchantRegister(OnlineReqDataVo vo) throws Exception {

        /** 1、业务数据转json*/
        String biz_content = JSONObject.toJSONString(vo.getParamData());
        logger.info("OnlineMercApi-merchantRegister-biz_content:"+biz_content);

        /** 2、组装入参的数据*/
        Map<String, String> params = new HashMap<String, String>();
        params.put("method","ysepay.merchant.register.accept");
        params.put("partner_id",vo.getPartnerId());
        params.put("timestamp", DateUtil.getDateNow());
        params.put("charset", Constants.CHARSET_UTF_8);
        params.put("sign_type",vo.getSignType());
        params.put("notify_url",vo.getNotifyUrl());
        params.put("version",version);
        params.put("biz_content",biz_content);

        /*3、对数据进行加签，并存入请求数据*/
        String sign = YsOnlineSignUtils.sign(params, vo.getPrivateKeyPassword(),vo.getPrivateKeyFilePath());
        logger.info("OnlineMercApi-merchantRegister-signData:"+sign);
        params.put("sign",sign);

        /*4、发送请求*/
        String result = HttpClientUtil.sendPostParam(vo.getReqUrl(),StringUtil.mapToString(params));
        logger.info("OnlineMercApi-merchantRegister-resultData:"+result);
        if(StringUtil.isEmpty(result)){
            logger.info("OnlineMercApi merchantRegister resultData is empty");
        }
        /*5、结果验签*/
        JSONObject jsonObject= JSON.parseObject(result, Feature.OrderedField);
        // 加签值
        String sinaValue = (String) jsonObject.get("sign");
        // 加签内容
        String content = jsonObject.get("ysepay_merchant_register_accept_response").toString();
        boolean flag = YsOnlineSignUtils.rsaCheckContent(content,sinaValue,Constants.CHARSET_UTF_8,vo.getYsPublicKeyFilePath());
        if(!flag){
            throw new Exception("check sign error");
        }
        return content;
    }

    /**
     *  主动查询商户注册信息
     */
    public static String queryMerchant(OnlineReqDataVo vo) throws Exception {

        /** 1、业务数据转json*/
        String biz_content = JSONObject.toJSONString(vo.getParamData());
        logger.info("OnlineMercApi-queryMerchant-biz_content:"+biz_content);

        /** 2、组装入参的数据*/
        Map<String, String> params = new HashMap<String, String>();
        params.put("method","ysepay.merchant.register.query");
        params.put("partner_id",vo.getPartnerId());
        params.put("timestamp", DateUtil.getDateNow());
        params.put("charset", Constants.CHARSET_UTF_8);
        params.put("sign_type",vo.getSignType());
        params.put("notify_url",vo.getNotifyUrl());
        params.put("version",version);
        params.put("biz_content",biz_content);

        /*3、对数据进行加签，并存入请求数据*/
        String sign = YsOnlineSignUtils.sign(params, vo.getPrivateKeyPassword(),vo.getPrivateKeyFilePath());
        logger.info("OnlineMercApi-queryMerchant-signData:"+sign);
        params.put("sign",sign);

        /*4、发送请求*/
        String result = HttpClientUtil.sendPostParam(vo.getReqUrl(),StringUtil.mapToString(params));
        logger.info("OnlineMercApi-queryMerchant-resultData:"+result);
        if(StringUtil.isEmpty(result)){
            logger.info("OnlineMercApi queryMerchant resultData is empty");
        }
        /*5、结果验签*/
        JSONObject jsonObject= JSON.parseObject(result, Feature.OrderedField);
        // 加签值
        String sinaValue = (String) jsonObject.get("sign");
        // 加签内容
        String content = jsonObject.get("ysepay_merchant_register_query_response").toString();
        boolean flag = YsOnlineSignUtils.rsaCheckContent(content,sinaValue,Constants.CHARSET_UTF_8,vo.getYsPublicKeyFilePath());
        if(!flag){
            throw new Exception("check sign error");
        }
        return content;
    }
}
